﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using pos.excption;
using NHibernate.Criterion;
using System.Collections;

namespace pos.entity
{
    public class Dao<T>
    {
        protected T obj;
        protected DetachedCriteria dc;

        virtual protected Object __AddDelegate(ISession session)
        {
            return session.Save(obj);
            
        }
        virtual protected object __UpdateDelegate(ISession session)
        {
            session.Update(obj);
            return true;
        }
        virtual  protected Object __DeleteDelegate(ISession session)
        {
            session.Delete(obj);
            
            //session.Delete(
            return true;
        }
        virtual protected Object __ListDelegate(ISession session)
        {
          
            ICriteria criteria=dc.GetExecutableCriteria(session);
            return criteria.List();
            //session.Delete(
            
        }
        virtual protected Object __DeleteByDcDelegate(ISession session)
        {
            ICriteria criteria = dc.GetExecutableCriteria(session);
            IList list = criteria.List();
            foreach (Object obj in list)
            {
                session.Delete(obj);
            }


            return true;
            //session.Delete(

        }
        virtual protected Object __GetByDcDelegate(ISession session)
        {
            ICriteria criteria=dc.GetExecutableCriteria(session);
            IList list= criteria.List();
            if (list.Count == 1)
                return list[0];
            if (list.Count >1)
                throw new DesignException("该纪录不止1条！" + dc.ToString());
            return null;


            //session.Delete(
            
        }
        virtual protected Object __UniqueResultDelegate(ISession session)
        {
            ICriteria criteria = dc.GetExecutableCriteria(session);
            return criteria.UniqueResult();
            //session.Delete(

        }

        public void Add(T obj)
        {
            this.obj = obj;
            dao.Execute(__AddDelegate);

        }
        public void Update(T obj)
        {
            this.obj = obj;
            dao.Execute(__UpdateDelegate); ;
        }
        public void Delete(T obj)
        {
            this.obj = obj;
            dao.Execute(__DeleteDelegate); ;
        }
        public DetachedCriteria DC
        {
            get { return DetachedCriteria.For(typeof(T)); }
        }
        public Object uniqueResult(DetachedCriteria dc)
        {
            this.dc = dc;
            return dao.Execute(__UniqueResultDelegate);
        }
        public IList List(DetachedCriteria dc)
        {
            if (dc == null)
                this.dc = DC;
            else
                this.dc = dc;

            return (IList)dao.Execute(__ListDelegate);
        }
        public void DeleteAll(DetachedCriteria dc)
        {
            if (dc == null)
                this.dc = DC;
            else
                this.dc = dc;

             dao.Execute(__DeleteByDcDelegate);
        }
        public T Get(DetachedCriteria dc)
        {
            this.dc = dc;
            return (T)dao.Execute(__GetByDcDelegate);
        }

       

        
        protected DaoTemplate dao;
        public DaoTemplate  DAO
        {
            get { return dao; }
            set { dao = value; }
        }
    }
}
